<?php

/**
 * downloaddata actions.
 *
 * @package    Redsea20
 * @subpackage downloaddata
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class downloaddataActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
   
  }
      public function executeSpeciesdownloaddata(sfWebRequest $request)
  {
    $str="";
    $speciesid=$request->getParameter('speciesid');
    $researchid=$request->getParameter('search');
      if (isset($researchid) && $researchid>0 && ! isset($speciesid))
      {
         $this->researchs = Research::getDownloadspecies($researchid,0);

      }
      else if (! isset($researchid) && isset($speciesid) && $speciesid>0)
      {
         $this->researchs = Research::getDownloadspecies(0,$speciesid);

      }


  }


  public function executeNonspeciesdownloaddata(sfWebRequest $request)
  {

       $str="";
    $speciesid=$request->getParameter('speciesid');
    $researchid=$request->getParameter('search');
      if (isset($researchid) && $researchid>0 && ! isset($speciesid))
      {
          $this->researchs = Research::getDownloadnonspecies($researchid,0);

      }
      else if (! isset($researchid) && isset($speciesid) && $speciesid>0)
      {
         $this->researchs = Research::getDownloadnonspecies(0,$speciesid);

      }


  }

    
public function executeZipData(sfWebRequest $request)
{
//echo $_SERVER['SERVER_NAME'];
//return sfView::NONE;

$temp_file = sfConfig::get('sf_upload_dir'); //. '/' . sfConfig::get('app_dir_zip');
$tmpname=$temp_file. "/". "zip";
if (is_dir($tmpname)) {
    $dir = $tmpname;
    $files = 0;

    $handle = opendir($dir) or die("Couldn't open the directory!");
    while ($file = readdir($handle)) {
        unlink($dir."/".$file);
        $files++;
    }
    closedir($dir);
}


  $overlayid=$request->getParameter('overlayid');
  $search=$request->getParameter('search');
       $str="";
 

      if (isset($overlayid) || isset($search))
      {
          

                if (!is_dir($tmpname)) {
                mkdir($tmpname, 0777);

                }
          
              $archive_file_name= $tmpname . "/". "rbis.zip";
                $zip = new ZipArchive();
                 if($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
                    exit("cannot open <$archive_file_name>\n");
                  }

              if (isset ($search) && $search!="")
              {
               $i = file_get_contents('http://'.$_SERVER['SERVER_NAME'].'/downloaddata/Speciesdownloaddata?search='.$search);

               

                $f = fopen($tmpname."/".'Species.xls','w+');
                fwrite($f,$i);
                fclose($f);
               if ($i!=null && $i!="")
                    $zip->addFile($tmpname."/".'Species.xls',"Species.xls");

                $i = file_get_contents('http://'.$_SERVER['SERVER_NAME'].'/downloaddata/Nonspeciesdownloaddata?search='.$search);
                //echo $i."aa";return sfView::NONE;
                $f = fopen($tmpname."/".'Attributes.xls','w+');
                fwrite($f,$i);
                fclose($f);
                if ($i!=null && $i!="")
                    $zip->addFile($tmpname."/".'Attributes.xls',"Attributes.xls");


                foreach (Research::getOverlayId($search,0) as $t)
                     {

//                        
                        $iData = file_get_contents('http://'.$_SERVER['SERVER_NAME'].'/downloaddata/overlayimagedownload?overlayid='.$t);
                        $iData=explode("OLYOLY", $iData);
                        $i=$iData[2];

			$f = fopen($tmpname."/".$iData[0].".".$iData[1],'w+');
	                fwrite($f,$i);
        	        fclose($f);
                	$zip->addFile($tmpname."/".$iData[0].".".$iData[1],$iData[0].".".$iData[1]);

                     }
              }
               
             
                      
                     
              else if (isset($overlayid) && $overlayid!="")
                     {

                         foreach (Research::getOverlayId(0,$overlayid) as $t)
                         {

			

                        $iData = file_get_contents('http://'.$_SERVER['SERVER_NAME'].'/downloaddata/overlayimagedownload?overlayid='.$t);
                        $iData=explode("OLYOLY", $iData);
                        $i=$iData[2];

			$f = fopen($tmpname."/".$iData[0].".".$iData[1],'w+');
	                fwrite($f,$i);
        	        fclose($f);
                	$zip->addFile($tmpname."/".$iData[0].".".$iData[1],$iData[0].".".$iData[1]);

                        
                         }
                     }
                     

                     



              
$zip->close();
                  header("Content-type: application/zip");
                  header("Content-Disposition: attachment; filename=rbis.zip");
                  header("Pragma: no-cache");
                  header("Expires: 0");
                  readfile("$archive_file_name");
                  exit;

          
      }


return sfView::NONE;
  }
  public function executeOverlayimagedownload(sfWebRequest $request)
  {
     $overlayid = 0;
      /**
       * IF PASSED PARAMETER IS NULL/0, THEN GET THE ID FROM SESSION
       */
      if($this->getRequestParameter('overlayid') != null && $this->getRequestParameter('overlayid') != '0')
      {
      $overlayid = $this->getRequestParameter('overlayid');
      $overlayList = Doctrine_Core::getTable('researchoverlay');
      $img = $overlayList->find($overlayid);
      //$entity = $entityList->find($this->getUser()->getAttribute('userid'));


      echo $img["overlaytitle"]."OLYOLY".$img["overlaytype"]."OLYOLY".$img["overlayimage"];
      }


      return sfView::NONE;
  }
  public function executeDownloadgraphdata(sfWebRequest $request)
  {
      $this->data=explode("|",$this->getRequestParameter('search'));

      $this->tag=$this->getRequestParameter('tag');
      



      return sfVIEW::SUCCESS;
      
  }

}
